<template>
  <div class="feedback">
    <el-button @click="addFeedShow = true" type="success" style="margin-bottom: 10px;">新增意见</el-button>
    <el-table :data="tableData" border>
      <el-table-column label="用户名称" prop="nickname" width="140" align="center"></el-table-column>
      <el-table-column label="联系电话" prop="mobilePhone" width="150" align="center"></el-table-column>
      <el-table-column label="反馈意见" prop="feedbackContent" min-width="200" align="center"></el-table-column>
      <el-table-column label="反馈时间" prop="feedbackTime" width="200" align="center"></el-table-column>
      <el-table-column label="处理结果" prop="feedbackContent" min-width="200" align="center"></el-table-column>
      <el-table-column label="状态" prop="status" width="80" align="center">
        <template slot-scope="scope">
          {{scope.row.status | statusFilter}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" :disabled="scope.row.status==1" @click="handleFeed(scope.row)">处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="新增意见" :visible.sync="addFeedShow" width="500px" :close-on-click-modal="false" :close-on-press-escape="false" :append-to-body="true">
      <el-form :model="feedForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="联系电话:" prop="mobilePhone">
          <el-input v-model="feedForm.mobilePhone" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="反馈意见:" prop="feedbackContent">
          <el-input v-model="feedForm.feedbackContent" size="mini" type="textarea" style="width: 300px;" rows="4"></el-input>
        </el-form-item>
      </el-form>
      <p style="text-align: center;">
        <el-button size="mini" type="primary" @click="submitFeed('ruleForm')">提交</el-button>
      </p>
    </el-dialog>

    <el-dialog title="意见处理" :visible.sync="handleFeedShow" width="500px" :close-on-click-modal="false" :close-on-press-escape="false" :append-to-body="true">
      <el-form :model="handleFeedForm">
        <el-form-item label="">
          <el-input v-model="handleFeedForm.remark" size="mini" type="textarea" style="width: 450px;" rows="4"></el-input>
        </el-form-item>
        <p style="text-align: center;">
          <el-button size="mini" type="primary" @click="handleFeedSubmit">提交</el-button>
        </p>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    filters: {
      statusFilter(status) {
        return status == 1 ? '已处理' : '未处理'
      }
    },
    data() {
      var validatorPhone = function (rule, value, callback) {
        if (value == undefined) {
          callback(new Error('手机号不能为空'))
        } else if (!/^1\d{10}$/.test(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        form: {
          currentPage: 1,
          pageSize: 10
        },
        tableData: [],
        addFeedShow: false,
        handleFeedShow: false,
        handleFeedForm: {},
        feedForm: {},
        rules: {
          mobilePhone: [{ required: true, validator: validatorPhone, trigger: 'blur' }],
          feedbackContent: [{ required: true, message: '请输入意见内容', trigger: 'blur' }],
        }
      }
    },

    mounted() {
      this.getTableData();
    },

    methods: {
      getTableData() {
        this.$axios({
          method: 'get',
          url: '/customer/feedback/page',
          params: this.form
        }).then((res) => {
          if(res.code == 200) {
            this.tableData = res.data.records;
          }
        })
      },

      handleFeed(row) {
        this.handleFeedShow = true;
        this.handleFeedForm.id = row.id;
      },

      handleFeedSubmit() {
        this.$axios({
          method: 'post',
          url: '/customer/feedback/update',
          data: this.handleFeedForm
        }).then((res) => {
          if(res.code == 200) {
            this.$message.success('处理成功');
            this.handleFeedShow = false;
            this.getTableData();
          } else {
            this.$message.warning(res.desc)
          }
        })
      },

      submitFeed(formName) {
        this.$refs[formName].validate((valid) => {
          if(valid) {
            this.$axios({
              method: 'post',
              url: '/customer/feedback/add',
              data: this.feedForm
            }).then((res) => {
              if(res.code == 200) {
                this.$message.success('新增意见成功');
                this.addFeedShow = false;
                this.getTableData();
              }
            })
          } else {

          }
        })
      }
    }
  }
</script>

<style scoped>
  .feedback {
    padding: 20px;
  }

  .el-input {
    width: 300px;
  }




</style>
